package start.demo01.fileIO;

import org.junit.Test;

import java.io.*;

public class FileManage {

    @Test
    public void main() throws Exception {
        forceDelDir(new File("D:\\file\\西安市"));
    }

    public static void cleanDir(String path) {//删除该目录下的所有空目录
        File file = new File(path);
        if (file.isDirectory()) {
            File[] files = file.listFiles();//遍历该目录下的文件和目录,不能递归遍历目录
            for (int i = 0; null != files && i < files.length; i++) {
                if (files[i].isDirectory()) {
                    boolean delete = files[i].delete();
                    if (delete) {
                        System.out.println("删除文件目录" + files[i].getPath());
                    } else {
                        System.out.println("目录不为空" + files[i].getPath());
                    }
                }
            }
        } else {
            System.out.println("不是目录" + file.getPath());
        }
    }

    @Test
    public void t1() {//删除文件,若删除后父文件夹是空目录，则删除父文件夹
        String path = "D:\\file\\de\\dwec.txt";
        File file = new File(path);
        file.delete();
        file.getParentFile().delete();
    }

    public static void cleanFile(String path) throws Exception {
       cleanFile(path,false);
    }
    /**
     * 清理文件夹或者文件
     * @param path 文件夹路径
     * @param bool 删除自身
     */
    public static void cleanFile(String path,boolean bool) throws Exception {
        File dir=new File(path);
        if(!dir.exists())
            return;
        if(bool){
            if(dir.isDirectory())
                forceDelDir(dir);
            else
                dir.delete();
        }else{
            if(dir.isDirectory()){
                File childFile[] = dir.listFiles();
                for (int i = 0; i < childFile.length; i++) {
                    if (childFile[i].isDirectory())
                        forceDelDir(childFile[i]);
                    else
                        childFile[i].delete();
                }
            }
        }
    }
    public static void forceDelDir(File dirFile) throws Exception { //删除目录下的所有文件以及当前文件夹
        if (dirFile.exists()) {
            try {
                File childFile[] = dirFile.listFiles();
                for (int i = 0; i < childFile.length; i++) {
                    if (childFile[i].isDirectory()) {
                        forceDelDir(childFile[i]);
                    } else {
                        childFile[i].delete();
                    }
                }
                // 删除目录
                dirFile.delete();
            } catch (Exception ex) {
                ex.printStackTrace();
                throw ex;
            }
        }
    }

    /**
     * 文件复制
     *
     * @param src
     * @param dish
     */
    public static void copy(File src, File dish) {
        if (!src.exists()) {
            throw new RuntimeException("文件不存在");
        }
        if (dish.exists())
            throw new RuntimeException("文件已存在");
        dish.getParentFile().mkdir();
        try {
            dish.createNewFile();
        } catch (IOException e) {
            throw new RuntimeException("文件创建失败");
        }
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(src));
             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dish))) {
            int len = -1;//reader.readLine();
            byte[] buffer = new byte[1024 * 1024];
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
        } catch (IOException e) {
            throw new RuntimeException("复制过程发生异常");
        }
    }
}
